import 'package:anythink_sdk/at_index.dart';

export 'package:anythink_sdk/at_index.dart';

class Rewarded {
  /// 加载激励广告
  Future<String> loadRewardedVideo(
      {required String placementID, String? keyword, String? userID}) {
    final params = {};
    if (keyword != null) {
      params[ATRewardedManager.kATAdLoadingExtraUserDataKeywordKey()] = keyword;
    }
    if (userID != null) {
      params[ATRewardedManager.kATAdLoadingExtraUserIDKey()] = userID;
    }
    return ATRewardedManager.loadRewardedVideo(
        placementID: placementID, extraMap: params);
  }

  /// 判断是否有广告缓存以及获取广告状态
  Future<bool> hasAdReady({required String placementID}) {
    // print('广告视频缓存');
    return ATRewardedManager.rewardedVideoReady(
      placementID: placementID,
    );
  }

  ///获取广告状态（返回值类型为Map） key-value如下：
  //
  // 1、isLoading：是否正在加载
  //
  // 2、isReady：是否有广告缓存
  //
  // 3、adInfo：当前优先级最高的广告缓存信息
  Future<Map> checkAdStatus({required String placementID}) {
    // print('广告视频状态$value');
    return ATRewardedManager.checkRewardedVideoLoadStatus(
      placementID: placementID,
    );
  }

  /// 获取当前广告位下所有可用广告的信息
  Future<String> getRewardedVideoValidAds({required String placementID}) {
    // print('激励视频当前广告位下$value');
    return ATRewardedManager.getRewardedVideoValidAds(placementID: placementID);
  }

  /// 展示激励视频
  Future<String> showAd({required String placementID}) {
    return ATRewardedManager.showRewardedVideo(placementID: placementID);
  }

  /// 当使用场景功能时，通过此api展示广告,参数为:广告位ID+场景ID(场景ID可通过Taku后台创建）
  Future<String> showSceneNativeAd(
      {required String placementID, required String sceneID}) async {
    return ATRewardedManager.showSceneRewardedVideo(
        sceneID: sceneID, placementID: placementID);
  }

  void listener() {
    ATListenerManager.rewardedVideoEventHandler.listen((value) {
      switch (value.rewardStatus) {
        //广告加载失败
        case RewardedStatus.rewardedVideoDidFailToLoad:
          print(
              "flutter rewardedVideoDidFailToLoad ---- placementID: ${value.placementID} ---- errStr:${value.requestMessage}");
          break;
        //广告加载成功
        case RewardedStatus.rewardedVideoDidFinishLoading:
          print(
              "flutter rewardedVideoDidFinishLoading ---- placementID: ${value.placementID}");
          break;
        //广告开始播放
        case RewardedStatus.rewardedVideoDidStartPlaying:
          print(
              "flutter rewardedVideoDidStartPlaying ---- placementID: ${value.placementID} ---- extra:${value.extraMap}");
          break;
        //广告结束播放
        case RewardedStatus.rewardedVideoDidEndPlaying:
          print(
              "flutter rewardedVideoDidEndPlaying ---- placementID: ${value.placementID} ---- extra:${value.extraMap}");
          break;
        //广告播放失败
        case RewardedStatus.rewardedVideoDidFailToPlay:
          print(
              "flutter rewardedVideoDidFailToPlay ---- placementID: ${value.placementID} ---- errStr:${value.extraMap}");
          break;
        //激励成功，建议在此回调中下发奖励
        case RewardedStatus.rewardedVideoDidRewardSuccess:
          print(
              "flutter rewardedVideoDidRewardSuccess ---- placementID: ${value.placementID} ---- extra:${value.extraMap}");
          break;
        //广告被点击
        case RewardedStatus.rewardedVideoDidClick:
          print(
              "flutter rewardedVideoDidClick ---- placementID: ${value.placementID} ---- extra:${value.extraMap}");
          break;
        //Deeplink
        case RewardedStatus.rewardedVideoDidDeepLink:
          print(
              "flutter rewardedVideoDidDeepLink ---- placementID: ${value.placementID} ---- extra:${value.extraMap} ---- isDeeplinkSuccess:${value.isDeeplinkSuccess}");
          break;
        //广告被关闭
        case RewardedStatus.rewardedVideoDidClose:
          print(
              "flutter rewardedVideoDidClose ---- placementID: ${value.placementID} ---- extra:${value.extraMap}");
          break;

        //广告开始播放（只针对穿山甲的再看一个广告）
        case RewardedStatus.rewardedVideoDidAgainStartPlaying:
          print(
              "flutter rewardedVideoDidAgainStartPlaying ---- placementID: ${value.placementID} ---- extra:${value.extraMap}");
          break;
        //广告结束播放（只针对穿山甲的再看一个广告）
        case RewardedStatus.rewardedVideoDidAgainEndPlaying:
          print(
              "flutter rewardedVideoDidAgainEndPlaying ---- placementID: ${value.placementID} ---- extra:${value.extraMap}");
          break;
        //广告播放失败（只针对穿山甲的再看一个广告）
        case RewardedStatus.rewardedVideoDidAgainFailToPlay:
          print(
              "flutter rewardedVideoDidAgainFailToPlay ---- placementID: ${value.placementID} ---- extra:${value.extraMap}");
          break;
        //激励成功（只针对穿山甲的再看一个广告）
        case RewardedStatus.rewardedVideoDidAgainRewardSuccess:
          print(
              "flutter rewardedVideoDidAgainRewardSuccess ---- placementID: ${value.placementID} ---- extra:${value.extraMap}");
          break;
        //广告被点击（只针对穿山甲的再看一个广告）
        case RewardedStatus.rewardedVideoDidAgainClick:
          print(
              "flutter rewardedVideoDidAgainClick ---- placementID: ${value.placementID} ---- extra:${value.extraMap}");

        case RewardedStatus.rewardedVideoUnknown:
          print("flutter rewardedVideoUnknown");
          break;
      }
    });
  }
}
